import 'package:changxindianzishangwu/cache/local_cache.dart';
import 'package:changxindianzishangwu/else/lib/common/index.dart';
import 'package:changxindianzishangwu/http/base/ec_base_request.dart';
import 'package:changxindianzishangwu/http/request/user_bind_appleid_request.dart';
import 'package:changxindianzishangwu/http/request/user_info_bind_phone_request.dart';
import 'package:changxindianzishangwu/http/request/user_info_check_phone_request.dart';
import 'package:changxindianzishangwu/http/request/user_info_loginout_request.dart';
import 'package:changxindianzishangwu/http/request/user_info_request.dart';
import 'package:changxindianzishangwu/http/request/user_info_update_pwd.dart';
import 'package:changxindianzishangwu/http/request/user_info_update_request.dart';
import 'package:changxindianzishangwu/http/request/user_unbind_request.dart';
import 'package:changxindianzishangwu/http/zd_net.dart';
import 'package:changxindianzishangwu/model/user_info_model.dart';
import 'package:changxindianzishangwu/model/base_model.dart';

import 'login_init_dao.dart';

class UserInfoDao {
  static getUserInfo({int userId}) async {
    var result;
    EcBaseRequest request = new UserInfoRequest();
    request.add("access_token", getAppLoginToken()).add("userId", userId ?? "");
    result = await ZdNet.getInstance().send(request);
    print("UserInfoDao:result:  ${result}");
    UserInfoModel userInfoModel = UserInfoModel.fromJson(result);
    if (userInfoModel.resultCode == 1){
      if(ObjectUtil.isEmpty(userId)){
        setUserinfo(result);
      }
      if(ObjectUtil.isNotEmpty(userId)&&getUserId()==userId){
        setUserinfo(result);
      }
    }
    return userInfoModel;
  }

  static updateUserInfo(String key, String name,
      { key1 = "",  name1,  key2 = "",  name2,key3="",name3}) async {
    var result;
    EcBaseRequest request = new UserInfoUpdateRequest();
    request.add("access_token", getAppLoginToken()).add(key, name ?? "");
    if (key1.length > 0) {
      request.add(key1, name1 ?? "");
    }
    if (key2.length > 0) {
      request.add(key2, name2);
    }
    if (key3.length > 0) {
      request.add(key3, name3);
    }
    result = await ZdNet.getInstance().send(request);
    print("updateUserInfoName:result:  ${result}");
    UserInfoModel userInfoModel = UserInfoModel.fromJson(result);
    if (userInfoModel.resultCode == 1) setUserinfoModel(userInfoModel.data);
    return userInfoModel;
  }

  static updateUserSettings(String key, int name) async {
    var result;
    EcBaseRequest request = new UserInfoUpdateRequest();
    request.add("access_token", getAppLoginToken()).add(key, name ?? "");
    result = await ZdNet.getInstance().send(request);
    print("updateUserInfoName:result:  ${result}");
    UserInfoModel userInfoModel = UserInfoModel.fromJson(result);
    if (userInfoModel.resultCode == 1) setUserinfoModel(userInfoModel.data);
    return userInfoModel;
  }

  static userLoginOut(String key, String phone) async {
    var result;
    EcBaseRequest request = new UserInfoLoginOutRequest();
    request
        .add("access_token", getAppLoginToken())
        .add("telephone", generateMd5(phone));
    result = await ZdNet.getInstance().send(request);
    print("updateUserInfoName:result:  ${result}");
    setUserinfo(result);
    return UserInfoModel.fromJson(result);
  }

  static userUpdatePwd(UserInfo userInfo, String nPwd, String code) async {
    var result;
    EcBaseRequest request = new UserInfoUpdatePwdRequest();
    request
        .add("telephone", userInfo.phone)
        .add("areaCode", userInfo.telephone.substring(0, 2))
        .add("newPassword", generateMd5(nPwd))
        .add("randcode", code);
    result = await ZdNet.getInstance().send(request);
    return result["resultCode"];
  }

  ///登录类型 0普通注册 1：QQ 2:微信 账号密码绑定手机号为0
  static userBindPhone(imOpenId, randcode, telephone, areaCode) async {
    EcBaseRequest request = new UserInfoBindPhoneRequest();
    request
        .add("access_token", getAppLoginToken())
        .add("loginInfo", imOpenId)
        .add("randcode", randcode)
        .add("telephone", telephone)
        .add("type", 0)
        .add("areaCode", areaCode);
    var result = await ZdNet.getInstance().send(request);

    return BaseModel.fromJson(result);
  }

  static checkUserPhone(account)async{
    EcBaseRequest request = new UserInfoCheckPhoneRequest();
    request
        .add("account", account)
        .add("verifyType", 0);
    var result = await ZdNet.getInstance().send(request);
    return BaseModel.fromJson(result);

  }

  ///type 2微信 3apple
  static binding(applyToken,type)async{
    EcBaseRequest request = new UserBindInfoRequest();
    request
        .add("access_token", getAppLoginToken())
        .add("applyToken", applyToken)
        .add("loginInfo", applyToken)
        .add("type", type);
    var result = await ZdNet.getInstance().send(request);
    return BaseModel.fromJson(result);

  }
  static unBind(type)async{
    EcBaseRequest request = new UserUnbindRequest();
    request
        .add("access_token", getAppLoginToken())
        .add("type", type);
    var result = await ZdNet.getInstance().send(request);
    return BaseModel.fromJson(result);

  }
}
